 /* 
  Copyright Software Engineering Research laboratory <serl@cs.wichita.edu>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public
 License along with this program; if not, write to the Free
 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
package wichita.edu.codereview;

import java.awt.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/*
 * 
 *  @author: Sara Bahrami <mxbahramizanjani@wichita.edu>
 */

/*
 * @purpose: Getting JSON object from GerritHttpRequest class and convert it to JSONArray and parse it to get required fields
 * @files , @messages, @author, @date,@status, @owner, @subject
 * called by WritingJsontoXML class
 * calls GerritHttpRequest class
 */
public class JsonArrayParser {
	//CONSTANTS
	//final private String url="https://git.eclipse.org/r/changes/?q=mylyn&o=ALL_REVISIONS&o=ALL_FILES&o=MESSAGES&n=1&N=002eaaa400006c6f";
	//MEMBER VARIABLES
	public JSONObject reviewDataObject=new JSONObject(); 
	public HashMap<Integer, String> fileVector= new HashMap<Integer, String>();
	public ArrayList<ArrayList<String>> reviewMessageInfo=new ArrayList<ArrayList<String>>();
	
	//CONSTRUCTOR
	
	public JsonArrayParser(String url) throws JSONException
	{
		GerritHttpRequest obj=new GerritHttpRequest(url);
		reviewDataObject=obj.getjson();
	}
	
	//This Method returns an HashMap includes all of unique files which are reviewed in the patch set 
	public HashMap<Integer,String> reviewPatchFiles() throws JSONException
	{
     	int fileVectorKey=1;
     	JSONObject revision = reviewDataObject.getJSONObject("revisions");
     	Iterator<String> revisionkeys = revision.keys();
     	while(revisionkeys.hasNext())
     	{
     		String revsionkey=revisionkeys.next();
     		JSONObject file=revision.getJSONObject(revsionkey).getJSONObject("files");
     		
     		
     		Iterator<String> filekeys = file.keys();
     		while(filekeys.hasNext())
     		{
     			String filekey=filekeys.next();
     			if(!(fileVector.containsValue(filekey))&&(filekey.endsWith(".java")))
     			//if(!(fileVector.containsValue(filekey)))
     			{
     				fileVector.put(fileVectorKey,filekey);
     				fileVectorKey++;

     			}

     			
     		}
     		
     	}
/*
     	for (Entry<Integer,String> entry:fileVector.entrySet())
     	{
     		System.out.println(entry.getKey());
     		System.out.println(entry.getValue());
     	}
     		
  */  
     	return fileVector;
	}
	
	//This Method returns a list of message, author and date which each reviewer wrote a comment in code review 
	public ArrayList<ArrayList<String>> reviewMessageExtractor() throws JSONException
	{
     	
		JSONArray messages = reviewDataObject.getJSONArray("messages");
     	for (int j=0;j<messages.length();j++)
     	{	
     		ArrayList<String> info=new ArrayList<String>();
     		
     		if(messages.getJSONObject(j).has("message"))
     		{
     			String message=messages.getJSONObject(j).getString("message").replaceAll("[\\r|\\n]","");
     			info.add(message);
     		}

     		
     		if(messages.getJSONObject(j).has("author"))
     			{ 
     			String author=messages.getJSONObject(j).getJSONObject("author").getString("name");
     			info.add(author);
     			}
     		if(messages.getJSONObject(j).has("date"))
     		{
         		String date=messages.getJSONObject(j).getString("date");
         		info.add(date);
     		}

     		//String pattern="yyyy-mm-dd";
     		//SimpleDateFormat format=new SimpleDateFormat(pattern);
     		//Date daterightformat=format.parse(date);
     		//System.out.println(message);
     		//System.out.println(author);
     		//System.out.println(daterightformat);
     		reviewMessageInfo.add(info);
     	}
     	return reviewMessageInfo;
	}
		

 	//return subject of codeReview
	public String getSubject() throws JSONException
	{
		String subject=reviewDataObject.getString("subject");
		return subject;
	}
	
 	//return owner of codeReview
	public String getOwner() throws JSONException
	{
		String owner=reviewDataObject.getJSONObject("owner").getString("name");
		return owner;
	}
	
 	//return status of codeReview
	public String getStatus() throws JSONException
	{
		String status=reviewDataObject.getString("status");
		return status;
	}
	
	//return more_changes flag of codeReview
	public Boolean getMoreChange() throws JSONException
	{
	 	Boolean more_changes=reviewDataObject.getBoolean("_more_changes");
		return more_changes;
	}
	
	public String getSortkey() throws JSONException
	{
	 	String sortkey=reviewDataObject.getString("_sortkey");
		return sortkey;
	}
	//return has more_changes flag of codeReview
	public Boolean hasMoreChange() throws JSONException
	{
	 	Boolean more_changes=reviewDataObject.has("_more_changes");
		return more_changes;
	}
	
}
